<?php



namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 * 聊友记录
 */
class UserChat extends Model
{
    protected $table = 'user_chat';

    protected $fillable = [
        'user_id', 'chat_user_id', 'flag'
    ];

    /**
     * 检测记录
     */
    static function check($userId = 0, $chatUserId = 0)
    {
        $where = ['user_id' => $userId, 'chat_user_id' => $chatUserId];
        $orWhere = ['user_id' => $chatUserId, 'chat_user_id' => $userId];
        return self::where($where)->orWhere(function ($query) use ($orWhere) {
            $query->where($orWhere);
        })->first();
    }

    /**
     * 插入聊友记录
     */
    static function addRecord($userId = 0, $chatUserId = 0)
    {
        if ($userChat = self::check($userId, $chatUserId)) return false;
        return self::create([
            'user_id' => $userId,
            'chat_user_id' => $chatUserId,
            'flag' => 1
        ]);
    }

    
}
